#!/usr/bin/env bash
set -e
function info { echo -e "\e[32m[info] $*\e[39m"; }
function warn  { echo -e "\e[33m[warn] $*\e[39m"; }
function error { echo -e "\e[31m[error] $*\e[39m"; exit 1; }

warn ""
warn "If you want more control over your own system, run"
warn "Home Assistant as a VM or run Home Assistant Core"
warn "via a Docker container."
warn ""

# Check if Modem Manager is enabled
if systemctl is-enabled ModemManager.service &> /dev/null; then
    warn "ModemManager service is enabled. This might cause issue when using serial devices."
fi

# Check dmesg access
if [[ "$(sysctl --values kernel.dmesg_restrict)" != "0" ]]; then
    info "Fix kernel dmesg restriction"
    echo 0 > /proc/sys/kernel/dmesg_restrict
    echo "kernel.dmesg_restrict=0" >> /etc/sysctl.conf
fi


dpkg-divert --package homeassistant-supervised --add --rename \
    --divert /etc/NetworkManager/NetworkManager.conf.real /etc/NetworkManager/NetworkManager.conf

dpkg-divert --package homeassistant-supervised --add --rename \
    --divert /etc/NetworkManager/system-connections/default.real /etc/NetworkManager/system-connections/default

dpkg-divert --package homeassistant-supervised --add --rename \
    --divert /etc/docker/daemon.json.real /etc/docker/daemon.json

dpkg-divert --package homeassistant-supervised --add --rename \
    --divert /etc/network/interfaces.real /etc/network/interfaces